package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.CheckInfoUtill;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 忘记密码activity（已经没有用了）
 */

public class ForgotPasswordPage extends Activity {

    private Button next_step_button;
    private RadioButton login_return;
    private EditText forget_phone;
    private SharedPrefsUtil sp;
    private String phone;
    private String validationPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forgot_password_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
        listener();
    }

    private void setData() {
        sp = new SharedPrefsUtil();
    }

    private void initView() {
        next_step_button = (Button) findViewById(R.id.next_step_button);
        login_return = (RadioButton) findViewById(R.id.login_return);
        forget_phone = (EditText) findViewById(R.id.forget_phone);
    }

    private void listener() {
        next_step_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = forget_phone.getText().toString();
                String url = AppUrl.URL + AppUrl.ReviseURL;
                Map<String,String> map=new HashMap<>();
                map.put("consumerTel", phone);
                XUtils.Get(url,map,new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String code = jsonObject.optString("code");
                            String pass = jsonObject.optString("pass");
                            if(CheckInfoUtill.isMobileNO(phone)){
                                if(code.equals("4699")){
                                    Intent intent = new Intent();
                                    intent.setClass(ForgotPasswordPage.this,ForgotPasswordPage2.class);
                                    intent.putExtra("phone",phone);
                                    intent.putExtra("pass",pass);
                                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                                    startActivity(intent);
                                }else{
                                    Toast.makeText(getApplicationContext(),"手机号未被注册",Toast.LENGTH_SHORT).show();
                                }
                            }else {
                                Toast.makeText(getApplicationContext(),"请输入正确的手机号",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                    }
                });
            }
        });

        login_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
